package com.mes.schedule.ScheduleOperation.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Repository;

import com.mes.common.dao.BasicDao;
import com.mes.schedule.ScheduleOperation.dao.iface.IOperationSplitBatchDao;
import com.mes.schedule.ScheduleOperation.domain.OperationSplitBatch;
import com.mes.schedule.domain.ScheduleOperationRecord;

@Repository
public class OperationSplitBatchDao extends BasicDao<OperationSplitBatch> implements IOperationSplitBatchDao {
	
	/**
	 * @author lcs
	 * @see the method use for OperationSplitBatchManger
	 * @param record
	 * @return
	 */
	public List<OperationSplitBatch> findChildOperation(
			ScheduleOperationRecord record) {
		List<OperationSplitBatch> recordList = new ArrayList<OperationSplitBatch>();

		try {

			Session session = this.getSession();
			Criteria crit = session.createCriteria(OperationSplitBatch.class);
			crit.add(Restrictions.eq("scheduleUid", record.getScheduleUid()));
			crit.add(Restrictions.eq("parentUniqueId", record
					.getScheduleOperationUid()));
			recordList = crit.list();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return recordList;
	}

	/**
	 * @author lcs
	 * @see the method use for OperationSplitBatchManger
	 * @param drawingId
	 * @param batchNum
	 * @param scheduleUid
	 * @return
	 */
	public List<OperationSplitBatch> findSplitOperation(String drawingId,
			String batchNum, String scheduleUid) {
		List<OperationSplitBatch> recordList = new ArrayList<OperationSplitBatch>();

		try {

			Session session = this.getSession();
			Criteria crit = session.createCriteria(OperationSplitBatch.class);
			crit.add(Restrictions.eq("scheduleUid", scheduleUid));
			crit.add(Restrictions.eq("drawingId", drawingId));
			crit.add(Restrictions.eq("newBatchNum", batchNum));
			recordList = crit.list();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return recordList;
	}
}
